/**
 * Created by zhanglihe on 16/1/15.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'datepicker'
], function ($, common, ready, Collection, datepicker) {

    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'partner/goods_count',
                condition: {},
                callback: function () {
                    loaded();//移除加载效果
                    //日期
                    $('.form_datetime').on('click', function () {
                        WdatePicker({
                            el: this.id,
                            dateFmt: 'yyyy-MM-dd',
                            maxDate: _this.collection.result._date
                        });
                    });
                    if ($('.form_datetime').val() == '') {
                        $('.form_datetime').val(_this.collection.result._date);
                    }
                    if (_this.collection.result.data.length == 0) {
                        $('#data-list').html('<center>无数据</center>');
                    }
                }
            });
            this.collection.fetch();
        },

        event: function () {
            var _this = this;
            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.condition.date = $('input[name="date"]').val();
                _this.collection.reload();
            });
            $('#btn-time').find('a').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.date = $(this).attr('rel');
                $(this).addClass('active').siblings().removeClass('active');
                $('input[name="keyword"]').val('');
                $('input[name="date"]').val('');
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.condition.date = $('input[name="date"]').val();
                _this.collection.reload();
            });
        }
    };
    new index();

});